﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;

namespace ZHDJ.Core.Util
{
    public class PageUtility
    {
        /// <summary>
        /// 注册资源文件
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="key">要搜索的客户端资源的键，防止</param>
        /// <param name="type">资源文件类型</param>
        public static void RegisterResource(Page page, string path, string key, ResType type)
        {
            string resStr = string.Empty;
            switch (type)
            {
                case ResType.Js:
                    resStr = string.Format("<script type=\"text/javascript\" language=\"javascript\" src=\"{0}\"></script>", path);
                    break;
                case ResType.Css:
                    resStr = string.Format("<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />", path);
                    break;
            }
            //是否已输出
            if (!page.ClientScript.IsClientScriptBlockRegistered(page.GetType(), key))
            {
                if (page.Header != null)
                {
                    LiteralControl link = new LiteralControl();
                    link.Text = "\r\n" + resStr;
                    page.Header.Controls.Add(link);
                }
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), key, "", false);//注册资源key
            }
        }

        public static void RegisterResource(Page page, string v1, string v2, object css)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 注册脚本块(或者样式块)
        /// </summary>
        /// <param name="script"></param>
        /// <param name="key"></param>
        /// <param name="type"></param>
        public static void RegisterScript(Page page, string script, string key)
        {
            //是否已输出
            if (!page.ClientScript.IsClientScriptBlockRegistered(page.GetType(), key))
            {
                if (page.Header != null)
                {
                    LiteralControl link = new LiteralControl();
                    link.Text = "\r\n" + script;
                    page.Header.Controls.Add(link);
                }
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), key, "", false);//注册资源key
            }
        }
        /// <summary>
        /// 页面跳转
        /// </summary>
        /// <param name="page"></param>
        /// <param name="webHost"></param>
        /// <param name="toType"></param>
        /// <param name="url"></param>
        /// <param name="para"></param>
        public static void RedirectTo(Page page,string webHost,string toType, string url, Action<Dictionary<string, string>> para = null)
        {
            
                var paraDic = new Dictionary<string, string>();
                if(para!=null)
                {
                    para(paraDic);
                }
               // para?.Invoke(paraDic);
                if (paraDic.Count > 0)
                {
                    var paral = paraDic.Keys.Select(item => string.Format("{0}={1}", item, paraDic[item])).ToArray();
                page.Response.Redirect(string.Format(webHost + "/RedirectTo.aspx?ToType={0}&{1}", toType,
                        string.Join("&", paral)));
                }
                else
                {
                page.Response.Redirect(string.Format(webHost + "/RedirectTo.aspx?ToType={0}", toType));
                }
            
        }

        public enum ResType
        {
            Css,
            Js
        }
    }
}
